package com.cqtd.rebate.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.joda.time.DateTime;

import com.cqtd.common.base.action.FooGenericAction;
import com.cqtd.common.base.utils.FooUtils;
import com.cqtd.common.util.GenerateRandomNumber;
import com.cqtd.common.util.StaticMethod;
import com.google.common.base.Charsets;
import com.opensymphony.xwork2.Preparable;

public class RebateAction extends FooGenericAction implements Preparable {

	private static final long serialVersionUID = 8185138006546030475L;

	public void prepare() throws Exception {
	}
	
	//消费返利结算
	public void rebateBilling()throws IOException{
		response.setCharacterEncoding(Charsets.UTF_8.toString());
		response.setContentType("text/plain");
		String result = "无记录";
		
		//1)从结算记录表中取出今日结算记录，如果为空，说明没有结算，今日可以进行结算，否则提示今日已结算，请不要重复结算！
		Date currentDate = new Date();
		String currentDay = FooUtils.toStandardDateNoTime(currentDate);
		String sql_billingrecord = "select * from cq_rebate_billingrecord where billingDate='"+currentDay+"'";
		List listbillingrecord = fooSpringJdbcService.queryForList(sql_billingrecord);
		if(FooUtils.nullToEmpty(listbillingrecord).size()>0){
			HashMap obj = (HashMap) listbillingrecord.get(0);
			String billingDetailDate = StaticMethod.nullObject2String(obj.get("billingDetailDate"));
			result = "今日已结算过,请不要重复结算! 结算详细时间为:"+StaticMethod.toNoDomDate(billingDetailDate);
		}else{//可以结算
			//2)取出以下基础数据
			//每新增一个会员每天拿来作为返利金额baseAmount
			double baseAmount = 1000.00;
			//取出今日有效会员总数userCount
			int userCount = 2;
			//取出今天新增的有效会员数 userNewCount
			int userNewCount = 1;
			//单个会员每天最高返利金额sysRebateAmountSingle
			double sysRebateAmountSingle = 25.00;
			//会员返利百分比 rebatePercent
			double rebatePercent = 0.10;
			//系统自动提现额度 sysAutoCashAmount
			double sysAutoCashAmount = 100.00;
			//消费者成为会员最低消费金额 beMemberMinAmount
			double beMemberMinAmount = 3600.00;
			//取当天详细日期 yyyy-MM-dd HH:mm:ss
			String currentDayTime = StaticMethod.toNoDomDate(FooUtils.toStandardDate(currentDate));
			//取出昨日单个会员消费返利金额yesterdayRebateAmountSingle
			double yesterdayRebateAmountSingle = 0.00;
			
			//当日单个会员最终返利金额 RealRebateAmountSingle 
			double RealRebateAmountSingle = 0.00;
			//3)从返利余额表中取出昨日的会员消费返利总余额yesterdayRebateBalanceSum
			double yesterdayRebateBalanceSum = 0.00;
			String yesterDay = currentDay;//昨天，使用datetime语法减去一天
			String sql_RebateBalance = "select rebateBalanceSum from cq_rebate_rebatebalance where rebateDate='"+yesterDay+"'";
			List listRebateBalance = fooSpringJdbcService.queryForList(sql_RebateBalance);
			if(FooUtils.nullToEmpty(listRebateBalance).size()>0){
				HashMap obj = (HashMap) listRebateBalance.get(0);
				yesterdayRebateBalanceSum = StaticMethod.nullObject2Double(obj.get("rebateBalanceSum"));
			}
			
			//4)计算当天每个会员应返利金额singleShouldRebateAmount 
			double singleShouldRebateAmount = 0.00;
			singleShouldRebateAmount = (yesterdayRebateBalanceSum + userNewCount*baseAmount)*rebatePercent/userCount;
			
			//当日单个会员消费返利余额 rebateBalanceSingle
			double rebateBalanceSingle = 0.00;
			//当日所有会员消费返利总余额rebateBalanceSum
			double rebateBalanceSum = 0.00;
			//当日单个会员实际返利金额rebateAmountSingle 
			double rebateAmountSingle = 0.00;
			//当日所有会员实际消费返利总金额rebateAmountSum
			double rebateAmountSum = 0.00;
			
			if(singleShouldRebateAmount>=sysRebateAmountSingle){
				rebateBalanceSingle = singleShouldRebateAmount - sysRebateAmountSingle;
				rebateBalanceSum = rebateBalanceSingle*userCount;
				if(yesterdayRebateAmountSingle==sysRebateAmountSingle){
					rebateAmountSingle = sysRebateAmountSingle*GenerateRandomNumber.generateRandomInt(99, 92)/100;
				}else{
					rebateAmountSingle = sysRebateAmountSingle;
				}
				rebateAmountSum = rebateAmountSingle*userCount;
			}else{
				rebateBalanceSingle = 0.00;
				rebateBalanceSum = 0.00;
				
				rebateAmountSingle = singleShouldRebateAmount;
				rebateAmountSum = singleShouldRebateAmount*userCount;
			}
			
			//系统提现
			
		}
		
		String sql = "select * from cq_rebate_myrebateinfo";
		@SuppressWarnings("rawtypes")
		List list = this.fooSpringJdbcService.queryForList(sql);
		if(FooUtils.nullToEmpty(list).size()>0){
			result = "操作成功";
		}
		response.getWriter().write(result);
	}
}
